#!/usr/bin/python

import cgi
import cgitb
import random
cgitb.enable()

form = cgi.FieldStorage()

x = random.randint(1, 10)
y = random.randint(1, 10)
z = 0
erg = 0
right = 0

if form.has_key("Zaehler"):
    z = z + int(form["Zaehler"].value)
z=z+1  

if z < 4 :
    erg = x + y
    o = '+'
else :
    erg = x * y
    o = '*'

if form.has_key("Richtige"):
    right = right + int(form["Richtige"].value)

if form.has_key("Eingabe"): 
#if "Eingabe" in form: #funktioniert auch so :D
    if form["Eingabe"].value==form["Ergebnis"].value:
        right = right +1




print "Content-Type: text/html\n"

print "<html>"
print "<body>"
print "<head><title>Kopfrechner</title></head>"
if z <=6:
    print "%i %s %i" % (x, o, y)
    print "<form action='kopfrechner.cgi' method='POST'>"
    print "Ihre Loesung:<br><input name='Eingabe' type='text' size='10' maxlength='4'/>"
    print "<input type='submit' value='Senden' />"
    print "<input type='hidden' name='ErsteZahl' value='%i'>" % x
    print "<input type='hidden' name='ZweiteZahl' value='%i'>" % y
    print "<input type='hidden' name='Zaehler' value=",z,">"
    print "<input type='hidden' name='Ergebnis' value='%i'>" %erg
    print "<input type='hidden' name='Richtige' value='%i'>" %right
    print "</form>"
else:
    print "Sie haben %i von %i Aufgaben richtig geloest." % (right, z-1)
print "</body>"
print "</html>"


